class ExamenesController < ApplicationController
  # MODULE: Ficha Médica

  #
  # GET /examenes
  # GET /examenes.xml
  def index
    @examenes = Examen.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @examenes }
    end
  end

  # GET /examenes/1
  # GET /examenes/1.xml
  def show
    @examen = Examen.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.js { render :layout => false}
      format.xml  { render :xml => @examen }
    end
  end

  # GET /examenes/new
  # GET /examenes/new.xml
  def new
    @examen = Examen.new
    @examen.usuario_id=params[:usuario_id]
    
    respond_to do |format|
      format.html # new.html.erb
      format.js { render :layout => false}
      format.xml  { render :xml => @examen }
    end
  end

  # GET /examenes/1/edit
  def edit
    @examen = Examen.find(params[:id])
    respond_to do |format|
      format.html # edit.html.erb
      format.js { render :layout => false}
    end
  end

  # POST /examenes
  # POST /examenes.xml
  def create
    @examen = Examen.new(params[:examen])
    @usuario = @examen.usuario
    respond_to do |format|
      if @examen.save
        format.html {
          flash[:notice] =  'Examen creada correctamente.'
          params[:oper].nil? ? redirect_to(@examen) : render(:text => "Ok")
        }
        format.js  {actualizar_view}
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @examen.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /examenes/1
  # PUT /examenes/1.xml
  def update
    @examen = Examen.find(params[:id])
    @usuario = @examen.usuario
    respond_to do |format|
      if @examen.update_attributes(params[:examen])
        format.html {
          flash[:notice] = 'Examen actualizado correctamente.'
          redirect_to(@examen)
        }
        format.js  {actualizar_view}
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @examen.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /examenes/1
  # DELETE /examenes/1.xml
  def destroy
    @examen = Examen.find(params[:id])
    @examen.destroy

    respond_to do |format|
      format.html {
        redirect_to(@examen.usuario)
      }
      format.js do
        render :update do |page|
          page["tr-examen-#{params[:id]}"].remove
        end
      end
    end
  end

  private
  def actualizar_view
    @examenes=@usuario.examenes
    @action_name="edit"
    render :update do |page|
      page.replace_html "examenes", :file => "examenes/index"
      page.visual_effect(:highlight, "tr-examen-#{params[:id]}", :duration=>2.5)
      page << "makeColorBox()"
    end
  end
end
